<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="'选择'"
    @ok="handleSubmit"
    @cancel="handelCancel"
  >
    <AnnoSearch
      ref="AnnoSearchRef"
      :anno-class-name="SearchAnnoClassName"
    ></AnnoSearch>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { BasicModal, useModalInner } from '@/components/Modal';
  import AnnoSearch from './AnnoSearch.vue';
  const emit = defineEmits(['success', 'register']);
  const SearchAnnoClassName = ref(false);
  const AnnoSearchRef = ref<InstanceType<typeof AnnoSearch> | null>(null);
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    const { annoClassName } = data;
    SearchAnnoClassName.value = toRaw(annoClassName);
    setModalProps({
      confirmLoading: false,
      title: '选择',
      width: 920,
      height: 600,
      okText: '确认',
      showOkBtn: true,
      cancelText: '取消',
    });
  });

  // const getTitle = computed(() => (!unref(isUpdate) ? '新增账号' : '编辑账号'));
  function handelCancel() {
    AnnoSearchRef.value!.clearSelectedRowKeys();
  }
  async function handleSubmit() {
    try {
      const rows = AnnoSearchRef.value!.getAddRows();
      AnnoSearchRef.value!.clearSelectedRowKeys();
      emit('success', rows);
      // const values = await validate();
      setModalProps({ confirmLoading: true });
      // TODO custom api
      closeModal();
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
